'''
    Goblinoid: Experience all of MediaGoblin on an Android Device
    Copyright (C) 2015 Dylan Jeffers

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
'''

from __future__ import print_function

from kivy.app import App
from kivy.properties import ObjectProperty, StringProperty, ListProperty
from kivy.lang import Builder
from kivy.uix.image import Image
from kivy.uix.anchorlayout import AnchorLayout
from kivy.uix.actionbar import ActionBar
from kivy.uix.togglebutton import ToggleButton
from kivy import utils

from goblinoid.root.goblinscreen import GoblinScreenWithManager
from goblinoid.root.goblinscreenmanager import GoblinScreenManager
from goblinoid.media.mediamodal import MediaModal

Builder.load_file('goblinoid/root/actionview.kv')


class OptionIcon(Image):

    def __init__(self, **kwargs):
        super(OptionIcon, self).__init__(**kwargs)
        self.bind(source=self.texture_update)


class OptionsButton(ToggleButton):
    state_normal_source = StringProperty()
    state_down_source = StringProperty()
    delegate = ObjectProperty()

    # may want to do on_release and check for state
    def on_release(self):
        self.delegate.on_button_click(self)

    def change_state(self):
        if self.state == 'normal':
            self.ids.icon.source = self.state_normal_source
        else:
            self.ids.icon.source = self.state_down_source


class OptionsBar(AnchorLayout):
    delegate = ObjectProperty()

    def on_button_click(self, button):
        self.delegate.on_option_bar_click(button.name)

    def set_button_states(self, button_name):
        for button in self.ids.values():
            if button.name == button_name:
                button.state = 'down'
            else:
                button.state = 'normal'


class GoblinBar(ActionBar):
    pass


class ActionScreenManager(GoblinScreenManager):

    def init_screens(self):
        profile_screen = self.screen_provider.get_screen(
            name='profile', class_name='profile')
        feed_screen = self.screen_provider.get_screen(
            name='feed', class_name='feed')

        self.add_widget(feed_screen)
        self.current = feed_screen.name
        self.add_widget(profile_screen)
        self.update_option_bar_state()

    def open_screen(self, **kwargs):
        name = kwargs['name']
        if self.current_screen is not None:
            self.screen_stack.add_screen(self.current)
        self.transition.direction = self.get_transition_dir(
            self.current_screen,
            self.get_screen(name))
        self.current = name
        self.update_option_bar_state()
        self.dispatch_on_return_to_current_screen()

    def open_previous_screen(self):
        self.screen_stack.remove_screen(self.current)
        if self.has_previous_screen():
            previous_screen_name = self.screen_stack.pop_screen()
            screen = self.get_screen(previous_screen_name)
            self.transition.direction = self.get_transition_dir(
                self.current_screen,
                screen)
            self.current = screen.name
            self.update_option_bar_state()
            self.dispatch_on_return_to_current_screen()

    def dispatch_on_return_to_current_screen(self, **kwargs):
        self.current_screen.dispatch('on_return', **kwargs)

    def update_option_bar_state(self):
        if self.current is not "None":
            self.delegate.update_option_bar_state(self.current)


class ActionLayout(GoblinScreenWithManager):

    goblin_bar_animation = ObjectProperty()
    max_color = ListProperty(utils.get_color_from_hex('#86D4B1'))
    min_color = ListProperty(utils.get_color_from_hex("#2a4237"))
    base_color = ListProperty([0, 0, 0, .1])
    error_color = ListProperty(utils.get_color_from_hex("#ef4832"))

    def __init__(self, **kwargs):
        super(ActionLayout, self).__init__(**kwargs)
        self.ids.sm.init_screens()

    def on_option_bar_click(self, button_name):
        if button_name == 'upload':
            self.open_media_modal()
        else:
            if not self.ids.sm.current == button_name:
                self.ids.sm.open_screen(name=button_name, class_name=button_name)

    def open_media_modal(self, **kwargs):
        media_modal = MediaModal()
        media_modal.open()

    def update_option_bar_state(self, button_name):
        self.ids.options_bar.set_button_states(button_name)
